﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ThumpThumpBus.Models.Domain;
using ThumpThumpBus.Models.Repository;
using ThumpThumpBus.Models.ViewModels;

namespace ThumpThumpBus.Controllers
{
    public class IncidentServiceController : ApiController
    {
        private readonly PersonRepository _personRepository = new PersonRepository();
        private readonly IncidentRepository _incidentRepository = new IncidentRepository();

        public IEnumerable<IncidentViewModel> Get()
        {
            var viewData = new List<IncidentViewModel>();
            var incidents = _incidentRepository.GetAllIncidents();
            foreach (var a in incidents)
            {
                var avm = new IncidentViewModel();
                avm.IncidentId = a.id;
                avm.IncidentReason = a.reason;
                avm.AssailantName = _personRepository.GetPersonById(a.assailantId).name;
                avm.VictimName = _personRepository.GetPersonById(a.victimId).name;
                viewData.Add(avm);
            }

            return viewData;
        }
        
        public HttpResponseMessage Post(Incident incident)
        {
            _incidentRepository.AddIncident(incident);

            var response = Request.CreateResponse(HttpStatusCode.Created, incident);
            
            var uri = Url.Link("DefaultApi", new { id = incident.id });
            response.Headers.Location = new Uri(uri);
            return response;
        }
    }
}
